package com.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.vo.UserVO;
import com.wanxi.service.UserService;
import com.wanxi.service.impl.UserServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/user/update")
public class UserUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        0.乱码问题
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        1.得到前端的数据
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String birthday = req.getParameter("birthday");
        String sex = req.getParameter("sex");
        String hobby = req.getParameter("hobby");
        String[] hobbys = req.getParameterValues("hobbys");
        String enable = req.getParameter("enable");
//          2.封装数据到VO对象
        UserVO userVO = new UserVO();
        userVO.setId(Tool.strToInt(id));
        userVO.setUsername(username);
        userVO.setPassword(password);
        userVO.setPhone(phone);
        userVO.setEmail(email);
        userVO.setBirthday(birthday);
        userVO.setSex(sex);
        userVO.setHobby(hobby);
        userVO.setEnable(Tool.strToInt(enable));
/*
多态：
1.子类父类的关系：继承或者实现
2.父类指向子类的引用
3.重写里面的方法
 */
//        3.调用服务逻辑层
        UserService userService = new UserServiceImpl();
//        4.得到返回结果
        ResultDTO result = userService.update(userVO);

//        System.out.println(username + "+++++" + password);

//        是否增加成功应该给前端页面一个反馈
//        5.将返回结果发送给前端
        resp.getWriter().println(JSONObject.toJSONString(result));

    }


}
